package it.unina.loquendo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{
	// Parametri per il debug
	private String TAG = "MainActivity";
	private boolean D = true;
	
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    private static final int COMPILE_GRAMMAR_REQUEST_CODE = 12345;
    private static final int TTS_REQUEST_CODE = 123456;

	private Button speakButton;
	private Button compileButton;
	private Button ttsButton;
	private TextView resultText;
	private TextView confidenceText;
	
	
	
    /** Funzione chiamata alla creazione dell'Activity */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Initialize the SPEAK button with a listener that for click events
        speakButton = (Button) findViewById(R.id.speak);         
        speakButton.setOnClickListener(new OnClickListener() {
            	public void onClick(View v) {
        	        if (v.getId() == R.id.speak) {
        	            startVoiceRecognitionActivity();
        	        }
            }});     
        
        
        compileButton = (Button) findViewById(R.id.compile);
        compileButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
    	        if (v.getId() == R.id.compile) {
    	            startCompileGrammarActivity();
    	        }
        }}); 
        
        ttsButton = (Button) findViewById(R.id.tts_button);
        ttsButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
    	        if (v.getId() == R.id.tts_button) {
    	        	TTS tts = TTS.getTTS();
    	        	tts.speak("Ciao come stai?");
    	        }
        }}); 
        
        resultText = (TextView) findViewById(R.id.results);         
        confidenceText = (TextView) findViewById(R.id.confidence);         

    }

    
    
    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {

		Intent intent = new Intent(getBaseContext(), ASRActivity.class);
	    intent.putExtra(Costants.EXTRA_GRAMMAR, "anm");
	    intent.putExtra(Costants.EXTRA_PROMPT, "ANM GRAMMAR");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
  
    }   
    
    
    
    /**
     * Fire an intent to start to compile a new file grammar
     * NB: the name of the file without .gram extension
     */
    private void startCompileGrammarActivity() {

		Intent intent = new Intent(getBaseContext(), ASRActivity.class);
	    intent.putExtra(Costants.EXTRA_COMPILE_GRAMMAR, "anm");
	    startActivityForResult(intent, COMPILE_GRAMMAR_REQUEST_CODE);
  
    }   
    
    
    
    /**
     * Fire intent for the TTS
     */
    private void startTTSActivity(String testo) {

		Intent intent = new Intent(getBaseContext(), TTSActivity.class);
	    intent.putExtra(Costants.EXTRA_TTS_TEXT, testo);
	    startActivityForResult(intent, TTS_REQUEST_CODE);
  
    }   
    
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case VOICE_RECOGNITION_REQUEST_CODE:        	
            // When the speech recognition returns
        	if (resultCode == Activity.RESULT_OK){
                // action to do when recognition is done 
        		LoquendoResult results = data.getExtras().getParcelable(Costants.EXTRA_RESULTS) ;        		
        		//float confidence = data.getExtras().getFloat(Costants.EXTRA_CONFIDENCE) ; 
        		//LoquendoResult semantica = data.getExtras().getParcelable(Costants.EXTRA_SEMANTICS);
        		resultText.setText(results.getResult() + " : " + results.getSemanticList());
        		confidenceText.setText(""+results.getConfidance());	
        		startTTSActivity(results.getResult());
        		super.onActivityResult(requestCode, resultCode, data);        		
        	}
        	break;
        	
        case COMPILE_GRAMMAR_REQUEST_CODE:        	
            // When activity returns
        	if (resultCode == Activity.RESULT_OK){ 
        		Toast.makeText(this, "Compiling Grammar - Completed!", Toast.LENGTH_LONG).show();       		
        	}
        	else if(resultCode == Activity.RESULT_CANCELED){
        		Toast.makeText(this, "Compiling Grammar - FAILED!", Toast.LENGTH_LONG).show();  
        	}
        	super.onActivityResult(requestCode, resultCode, data);
        	break;	
        }
    }
    
}
